import datetime
import math

class MouseClickHelper():

    def __init__(self) -> None:
        # for double click...
        self.NumberOfClicks = 0
        self.LastMouseX = 0
        self.LastMouseY = 0
        self.LastMouseBtn = -1
        self.LastTimestamp = datetime.datetime.now()        
        self.DoubleClickDistance = 5
        self.DoubleClickInterval = 0.3  #seconds

    # do click, return number of clicks
    def Click(self, x, y, btn) -> int:
        self.NumberOfClicks += 1
        if self.NumberOfClicks > 2:
            self.NumberOfClicks = 1

        # get distance snce last click
        xdist = self.LastMouseX - x
        ydist = self.LastMouseY - y
        moveDistance = math.sqrt(xdist * xdist + ydist * ydist)

        # get time elapsed
        now = datetime.datetime.now()
        elapsed = (now - self.LastTimestamp).total_seconds()

        if self.NumberOfClicks == 2:
            if self.LastMouseBtn != btn or \
                    elapsed > self.DoubleClickInterval or \
                    moveDistance > self.DoubleClickDistance:            
                self.NumberOfClicks = 1

        self.LastMouseX = x
        self.LastMouseY = y
        self.LastMouseBtn = btn
        self.LastTimestamp = now
        
        return self.NumberOfClicks
